﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using System.Text;
using System.Security.Cryptography;

namespace FastDeal.Control
{
    public partial class wuc_ThemTaiKhoan : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Label1.Visible = false;
                lberuser.Visible = false;
                lberemail.Visible = false;
                lbercmnd.Visible = false;
            }
        }
        TaiKhoan_BUS tkBUS = new TaiKhoan_BUS();

        private bool KiemTraUserDangKy(string username)
        {
            if (tkBUS.KiemTraUserDangKy(username))
            {
                return false;
            }
            else
                return true;
        }

        // kiểm tra user
        private bool KiemTraUserDangKy()
        {
            if (tkBUS.KiemTraUserDangKy(txtUserName.Text) == false)
            {
                return false;
            }
            else
                return true;
        }

        // kiểm tra email
        private bool KiemTraEmailDangKy()
        {
            if (tkBUS.KiemTraEmailDangKy(txtEmail.Text) == false)
            {
                return false;
            }
            else
                return true;
        }

        // kiểm tra cmnd có trùng ko
        public bool KiemTraCMNDDangKy()
        {
            if (tkBUS.KiemTraCMNDDangKy(txtCMND.Text) == false)
            {
                return false;
            }
            else
                return true;
        }
        public string MaHoaMatKhau(string password)
        {
            UnicodeEncoding encoding = new UnicodeEncoding();
            Byte[] hashBytes = encoding.GetBytes(password);
            // Compute the SHA-1 hash
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            Byte[] cryptPassword = sha1.ComputeHash(hashBytes);
            return BitConverter.ToString(cryptPassword).Substring(0, 8);
        }

        private bool DangKy1(int maloaitv, string ghichu)
        {
            TaiKhoan_BUS tkBUS = new TaiKhoan_BUS();
            ThongTinTaiKhoan_BUS tttkBUS = new ThongTinTaiKhoan_BUS();
            TaiKhoan tk = new TaiKhoan();
            ThongTinTaiKhoan tttk = new ThongTinTaiKhoan();
            List<TaiKhoan> listtk = new List<TaiKhoan>();

            if (KiemTraUserDangKy())
            {
                if (KiemTraEmailDangKy())
                {
                    if (KiemTraCMNDDangKy())
                    {
                          tk.UserName = txtUserName.Text;
                            tk.Password = MaHoaMatKhau(txtPassword.Text);
                            tk.UserName = txtUserName.Text;
                            tk.MaLoaiThanhVien = maloaitv;
                            tk.TrangThai = 1;
                            tk.HoatDongCuoiCung = Convert.ToDateTime(DateTime.Now.ToString());
                            tk.isDelete = 0;
                            tk.Email = txtEmail.Text;
                            tkBUS.Them_TaiKhoan(tk);        // chèn vào bảng tài khoản

                            tttk.UserID = tk.UserID;
                            tttk.HoTen = txtHoTen.Text;
                            tttk.Nickname = txtNickName.Text;
                            tttk.DiaChi = txtDiaChi.Text;
                            tttk.DienThoai = txtDienThoai.Text;
                            tttk.CMND = txtCMND.Text;
                            tttk.GhiChu = ghichu;
                            tttk.isDelete = 0;

                            if (tttkBUS.Them_ThongTinTaiKhoan(tttk))
                            {
                                Label1.Text = "Tạo mới thành công!";
                                Label1.Visible = true;
                            }
                      
                    }
                    else
                    {
                        lbercmnd.Text = "CMND đã tồn tại.";
                        lbercmnd.Visible = true;
                        return false;
                    }
                }
                else
                {
                    lberemail.Text = "Email đã tồn tại.";
                    lberemail.Visible = true;
                    return false;
                }
            }
            else
            {
                lberuser.Text = "Username đã tồn tại.";
                lberuser.Visible = true;
                return false;
            }
            return true;
        }

      
        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Visible = false;
            lberuser.Visible = false;
            lberemail.Visible = false;
            lbercmnd.Visible = false;

            TaiKhoan_BUS tkBUS = new TaiKhoan_BUS();
            ThongTinTaiKhoan_BUS tttkBUS = new ThongTinTaiKhoan_BUS();
            TaiKhoan tk = new TaiKhoan();
            ThongTinTaiKhoan tttk = new ThongTinTaiKhoan();
            List<TaiKhoan> listtk = new List<TaiKhoan>();

            // doanh nghiệp là 3
            tk.MaLoaiThanhVien = 3;
            tttk.GhiChu = "Tài khoản doanh nghiệp";
            if (DangKy1(tk.MaLoaiThanhVien, tttk.GhiChu))
            {
                Label1.Visible = true;
                txtUserName.Text = String.Empty;
                txtEmail.Text = String.Empty;
                txtHoTen.Text = String.Empty;
                txtNickName.Text = String.Empty; ;
                txtCMND.Text = String.Empty;
                txtDienThoai.Text = String.Empty; ;
                txtDiaChi.Text = String.Empty;
            }
                    
        }
    }
}